package com.admin.action

import com.admin.service.AccountAdminService
import com.data.annotation.NoargAnnotation
import com.data.annotation.OpenAnnotation
import com.data.bean.Account
import com.data.util.JsonUtils
import org.apache.log4j.Logger
import org.omg.CORBA.Object
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.ResponseBody

import javax.servlet.http.HttpServletRequest

/**
 * 账号管理
 */
@Controller
@OpenAnnotation
@NoargAnnotation
@RequestMapping("/accountAdmin/admin/")
class AccountAdminAction {
    @Autowired
    private var accountAdminService: AccountAdminService? = null

    /**
     * 账号列表
     *
     * @param request
     * @return
     */
    @GetMapping("list")
    fun accountListAction(request: HttpServletRequest): String? {
        val list = accountAdminService?.accountListService()
        request.setAttribute("dataMap", list)
        return "WEB-INF/admin/account/accountList"
    }

    /**
     * 删除账号
     *
     * @param id
     * @param request
     * @return
     */
    @PostMapping("delete")
    @ResponseBody
    fun deleteAccountAction(id: String, request: HttpServletRequest): Map<String, Any> {
        val bool = accountAdminService!!.deleteAccountService(id)
        return JsonUtils.returnMassageUtils(bool, if (bool) "删除成功" else "删除失败")
    }

    /**
     * 修改账号状态
     *
     * @param id
     * @param state   账号的当当前状态
     * @param request
     * @return
     */
    @PostMapping("change")
    @ResponseBody
    fun changeAccountAction(id: String, state: Boolean, request: HttpServletRequest): Map<String, Any> {
        val bool = accountAdminService!!.changeAccountService(id, state)
        return JsonUtils.returnMassageUtils(bool, if (bool) "修改成功" else "修改失败")
    }

    /**
     * 进入到添加界面
     */
    @GetMapping("addinput")
    fun addAccountAction(request: HttpServletRequest): String = "WEB-INF/admin/account/addaccount"

    /**
     * 手工创建账号,如果创建成功，就直接返回到当前界面，如果创建失败，就直接返回到404界面
     *
     * @param account 账号实体，包括用户名，邮箱，手机号，身份，状态
     * @param request
     * @return
     */
    @PostMapping("add")
    @ResponseBody
    fun addAccountAction(username: String, email: String, phone: String, types: Int, request: HttpServletRequest): Map<String, Any> {
        val account = Account(id = "", username = username, password = "", email = email, phone = phone, types = types, state = false)
        val bool = accountAdminService!!.addAccountService(account)
        return JsonUtils.returnMassageUtils(bool, if (bool) "添加成功" else "添加失败")
    }

    /**
     * @param username 用户名，邮箱，手机号
     * @param type     检测类型，email，username，phone
     * @param request
     * @return 如果是true，则表示验证通过，如果是false表示验证不通过
     */
    @RequestMapping("check")
    @ResponseBody
    fun checkAccountAction(username: String, type: String, request: HttpServletRequest): Map<String, Any> {
        val bool = accountAdminService!!.checkAccountService(username, type)
        return JsonUtils.returnMassageUtils(bool, type + if (bool) "可用" else "已经被占用")
    }
}
